package com.eca.emas.daemon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

import com.eca.emas.daemon.shared.Sensor;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class SSHConnector {

	private static final String STATE_SENSOR_FOUND = "sensorfound";
	private static final String STATE_NORMAL = "normal";

	String sshPassword;
	String hostOrIP;

	public SSHConnector(String sshPassword, String hostOrIP) {
		this.sshPassword = sshPassword;
		this.hostOrIP = hostOrIP;
	}

	public ArrayList<Sensor> getAllSensors() throws IOException, JSchException {
		ArrayList<Sensor> sensors = new ArrayList<Sensor>();
		if (sshPassword != null && hostOrIP != null) {
			if (!sshPassword.isEmpty() && !hostOrIP.isEmpty()) {
				JSch jSch = new JSch();
					Session session = jSch.getSession("user", hostOrIP, 22);
					session.setUserInfo(new UserInfo() {

						@Override
						public void showMessage(String arg0) {
						}

						@Override
						public boolean promptYesNo(String arg0) {
							return true;
						}

						@Override
						public boolean promptPassword(String arg0) {
							return true;
						}

						@Override
						public boolean promptPassphrase(String arg0) {
							return true;
						}

						@Override
						public String getPassword() {
							return sshPassword;
						}

						@Override
						public String getPassphrase() {
							return null;
						}
					});
					session.connect();

					Channel channel = session.openChannel("shell");

					PrintWriter writer = new PrintWriter(
							channel.getOutputStream());
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(channel.getInputStream()));

					channel.connect();

					sensors = getSensors(writer, reader);

					writer.close();
					reader.close();

					channel.disconnect();
					session.disconnect();
				} 
		}
		return sensors;

	}

	private void getNameForSensor(Sensor sensor, PrintWriter writer,
			BufferedReader reader) {
		String state = STATE_NORMAL;
		String sensorName = "";
		boolean sensorNameFound = false;

		writer.println("cat /etc/wiregate/owsensors.conf");
		writer.flush();

		String line = "";
		if (sensor != null) {
			if (!sensor.getSensorID().isEmpty()) {
				try {
					while (!sensorNameFound
							&& (line = reader.readLine()) != null) {
						if (state == STATE_NORMAL) {
							if (line.contains(sensor.getSensorID())) {
								state = STATE_SENSOR_FOUND;
							}
						} else if (state == STATE_SENSOR_FOUND) {
							if (line.contains("name")) {
								sensorName = line.substring(
										line.indexOf("=") + 2, line.length());
								sensor.setSensorName(sensorName);
								sensorNameFound = true;
								state = STATE_NORMAL;
							}
						}

					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public ArrayList<Sensor> getSensors(PrintWriter writer,
			BufferedReader reader) {
		ArrayList<Sensor> sensors = new ArrayList<Sensor>();

		if (writer != null && reader != null) {
			writer.println("cd /var/www/rrd/");
			writer.println("ls -1 > /home/user/sensoren.emas");
			writer.println("cat /home/user/sensoren.emas");
			writer.println("rm /home/user/sensoren.emas");
			writer.flush();
		}

		try {
			String line = "";
			boolean tryReading = true;
			while (tryReading && ((line = reader.readLine()) != null)) {
				if (line.contains("_temp.rrd")) {
					String sensorID = line.substring(0, line.indexOf("_"));
					Sensor sensor = new Sensor();
					sensor.setSensorID(sensorID);
					sensors.add(sensor);
				} else if (!sensors.isEmpty() && !line.contains("_temp.rrd")) {
					tryReading = false;
				}
			}

			if (sensors != null && !sensors.isEmpty()) {
				for (Sensor sensor : sensors) {
					getNameForSensor(sensor, writer, reader);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return sensors;
	}

}
